<template>
    <el-dialog @close="cancleAddStepItem" v-model="dialogVisible" width="30%" title="新增操作步骤信息" destroy-on-close center>
        <template #default>
            <el-form :model="stepItem" label-width="100px" label-position="left">
                <el-form-item label="顺序编号">
                    <el-input type="number" v-model="stepItem.processStepCode" aria-placeholder="顺序编号" />
                </el-form-item>
                <el-form-item label="材料URL">
                    <el-input v-model="stepItem.processStepUrl" aria-placeholder="材料URL" />
                </el-form-item>
                <el-form-item label="辅助设备">
                    <el-input v-model="stepItem.processStepDevice" aria-placeholder="辅助设备" />
                </el-form-item>
                <el-form-item label="辅助材料">
                    <el-input v-model="stepItem.processStepMaterial" aria-placeholder="辅助材料" />
                </el-form-item>
                <el-form-item label="步骤说明">
                    <el-input type="textarea" v-model="stepItem.processStepDescription" aria-placeholder="步骤说明" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="stepItem.processStepNotes" aria-placeholder="备注" />
                </el-form-item>
            </el-form>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="12">
                    <el-button type="primary" @click="addStepItem(processId)">确定</el-button>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="cancleAddStepItem">取消</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>
<script>
import { watch } from "vue";
// 引入我们申明的process的api的js
import StepItemRequest from '../../../../api/processStep.js';
export default {
    props: ["visible","processId"],
    emits: ["optionCompleted"], // 对外暴露一个事件，用来传递添加学生的操作结果
    data() {
        return {
            stepItem: {
                processId:this.processId
            },// 绑定新增的操作步骤信息
            dialogVisible: false,// 添加操作步骤窗口的显示与隐藏
        }
    },
    methods: {
        /**
         * 取消添加操作步骤项的事件
         */
        cancleAddStepItem(){
            this.stepItem = {};
            // 通知父组件  操作完成，让父组件关闭窗体
            this.$emit("optionCompleted");
        },
        /**
         * 添加操作步骤项信息
         */
        addStepItem(){
            StepItemRequest.addStepItem(this.stepItem,res=>{
                if (res.code == '200'){
                    this.$message({
                        type: 'success',
                        message: '添加操作步骤成功!'
                    });
                    this.stepItem = {};
                    // 通知父组件  操作完成，让父组件关闭窗体
                    this.$emit("optionCompleted");

                }else if(res.code == '300'){
                    this.$message({
                        type: 'warning',
                        message: res.msg
                    });
                }else{
                    this.$message({
                        type: 'error',
                        message: '添加操作步骤失败!'
                    });
                }
            });
        },

    }, mounted(props) {
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        });
        watch(() => this.processId, (newVal, oldVal) => {
            this.stepItem.processId = newVal;
        });
    }
}
</script>